package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;

/**
 * Create by ws.
 * on Data:2022/2/13  Time:下午 5:07.
 * description: ** .
 */
public class PicShowView extends ViewGroup {
    private int spacing= SizeUtils.dp2px(5);
    private int itemWh;
    public PicShowView(Context context) {
        this(context,null);
    }

    public PicShowView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PicShowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=measureWidth(widthMeasureSpec);
        itemWh=(width-spacing*2)/3;
        if(getChildCount()==1){
            View view=getChildAt(0);
            LayoutParams params=view.getLayoutParams();
            params.width=width;
            params.height=itemWh*2;
            view.setLayoutParams(params);
            measureChild(view,widthMeasureSpec,heightMeasureSpec);
        }else {
            for (int i = 0; i < getChildCount(); i++) {
                View view=getChildAt(i);
                LayoutParams params=view.getLayoutParams();
                params.width=itemWh;
                params.height=itemWh;
                view.setLayoutParams(params);
                measureChild(view,widthMeasureSpec,heightMeasureSpec);
            }
        }
        int height= measureHeight(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = specSize;
        if (specMode == MeasureSpec.AT_MOST) {
            result = ScreenUtils.getAppScreenWidth();
        } else {
            result = specSize;
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        //int specMode = MeasureSpec.getMode(measureSpec);
       // int specSize = MeasureSpec.getSize(measureSpec);
        int result = 0;
        int line=getRow(getChildCount());
        int h=0;
        if(getChildCount()==1){
            h=itemWh*2;
        }else {
            h=line*itemWh+(line-1)*spacing;
        }
        result = h;
        return result;
    }
    private int column=3;
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(getChildCount()==1){
            View view= getChildAt(0);
           view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
        }else {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                int childWidth = itemWh;
                int childHeight = itemWh;
                int childColumn = i % 3;//第几列
                int childRow = getRow(i + 1);
                int shiftX = childColumn * spacing;
                int shiftY = (childRow - 1) * spacing;
                if (childColumn + 1 == column) {
                    child.layout(childColumn * childWidth + shiftX, (childRow - 1) * childHeight + shiftY, (childColumn + 1) * childWidth + shiftX + spacing, childRow * childHeight + shiftY);
                } else
                    child.layout(childColumn * childWidth + shiftX, (childRow - 1) * childHeight + shiftY, (childColumn + 1) * childWidth + shiftX, childRow * childHeight + shiftY);
            }
        }
    }
    private int getRow(int count) {
        int s = count % column > 0 ? 1 : 0;
        int row = count / column + s;
        return row;
    }
}
